// pages/andon/real_time_kanban/real_time_kanban.js
const mybehavior = require("../../../behaviors/requests")
var taskId = null;
Page({

  /**
   * 页面的初始数据
   */
  behaviors: [mybehavior],
  data: {
    page: 1,
    total: 0,
    page_all: 0,
    base_url: "https://logisticsengineering.gree.com/andon_system",
    location: "车间汇总",
    factory: '西区家用总装',
    factories: [],
    list: [],
    option1: [
      { text: '车间汇总', value: '车间汇总' }
    ],
    value1: '',
    show: false,
    detail_list: [],
    steps: [
      {
        text: '创建中',
        desc: '',
      },
      {
        text: '正在上料',
        desc: '',

      },
      {
        text: '前往取料',

      },
      {
        text: '正在拉料',
        desc: '',

      },
      {
        text: '确认收料',
        desc: '',

      },
      {
        text: '已完成',
        desc: '',

      },
    ],
    title: "一车间汇总",
    title1: "",
    title2: "",
    title3: "",
    title4: "上一次",
    active: 0

  },
  pageUp() {
    let now_page = this.data.page
    if (now_page != 1) {
      now_page = now_page - 1
      this.setData({
        page: now_page,
      })
      this.getDatas()
    }


  },
  pageDown() {
    let now_page = this.data.page
    if (now_page < this.data.page_all) {
      now_page = now_page + 1
      this.setData({
        page: now_page
      })
      this.getDatas()
    }

  },
  // 切换上次、本次详情
  detailSelect() {
    let t = this.data.title4
    let data_arr = this.data.detail_list
    let step_arr = this.data.steps
    if (t == "上一次") {
      this.setData({
        title4: "回到本次",
        title3: data_arr[1].all_times
      })
      step_arr[0].desc = data_arr[1].start_time
      step_arr[1].desc = data_arr[1].state_time1
      step_arr[2].desc = data_arr[1].state_time2
      step_arr[3].desc = data_arr[1].state_time3
      step_arr[4].desc = data_arr[1].state_time4
      step_arr[5].desc = data_arr[1].over_time
      this.setData({
        steps: step_arr,
        active: 5
      })
    } else {
      this.setData({
        title3: data_arr[0].all_times,
        title4: "上一次"
      })
      step_arr[0].desc = data_arr[0].start_time
      step_arr[1].desc = data_arr[0].state_time1
      step_arr[2].desc = data_arr[0].state_time2
      step_arr[3].desc = data_arr[0].state_time3
      step_arr[4].desc = data_arr[0].state_time4
      step_arr[5].desc = data_arr[0].over_time
      this.setData({
        active: data_arr[0].step,
        steps: step_arr
      })
    }
  },
  // 详情弹出框
  onCaoZuo(e) {
    let row = e.currentTarget.dataset.row_data
    console.log(row);
    this.setData({
      show: true,
      title1: row.location,
      title2: row.material_name,
      title3: row.all_times,
    })
    let that = this
    let url = this.data.base_url + "/west_progress_bar"
    let data = {
      location: this.data.value1,
      material_location: row.location,
      material_name: row.material_name,
      current: 1,
    }
    this.get(url, data).then((res) => {
      console.log(res)
      const result = res;
      const code = res.code;
      if (code == 20000) {
        that.setData({
          detail_list: result.pop_data
        })
        console.log(that.data.detail_list);
        let step_arr = that.data.steps
        step_arr[0].desc = that.data.detail_list[0].start_time
        step_arr[1].desc = that.data.detail_list[0].state_time1
        step_arr[2].desc = that.data.detail_list[0].state_time2
        step_arr[3].desc = that.data.detail_list[0].state_time3
        step_arr[4].desc = that.data.detail_list[0].state_time4
        step_arr[5].desc = that.data.detail_list[0].over_time
        that.setData({
          steps: step_arr,
          active: that.data.detail_list[0].step
        })

      } else {
        wx.showToast({
          title: result.msg,
          icon: 'error'
        })
      }
    }).catch((err) => {
      console.log(err)
      wx.showToast({
        title: '服务器出现错误！',
        icon: 'error'
      })
    })
  },

  onClose() {
    this.setData({
      show: false,
      title1: "",
      title2: "",
      title3: "",
      title4: "上一次",
      active: 0
    })
  },
  // 下拉筛选框
  onDropdownChange(e) {
    this.getDatas()
    this.setData({
      title: e.detail,
      page: 1
    })
  },
  // 获取线体 
  getLines() {
    const that = this
    const url = "https://logisticsengineering.gree.com/andon_system/get_line_list"
    const data = {
      "factory": that.data.factory
    }
    this.get(url, data).then((res) => {
      console.log(res, data)
      let options = [{ text: "车间汇总", value: "车间汇总" }]
      for (let i = 0; i < res.data.length; i++) {
        let option = {};
        option.value = res.data[i];
        option.text = res.data[i];
        options.push(option);
      }
      that.setData({
        option1: options
      })
    }).catch((err) => {
      console.log(err)
    })
  },
  // 获取数据
  getDatas() {
    let that = this
    let url = "https://logisticsengineering.gree.com/andon_system/change_real_time_table"
    let data = {
      location: that.data.value1,
      current: that.data.page,
      factory: that.data.factory
    }
    this.get(url, data).then((res) => {
      const result = res;
      const code = result.code;
      if (code == 20000) {
        that.setData({
          list: result.data.table_data,
          total: result.data.table_total,
          page_all: Math.ceil(result.data.table_total / 10)
        })
      } else {
        wx.showToast({
          title: result.msg,
          icon: 'error'
        })
      }
    }).catch((err) => {
      console.log(err);
      wx.showToast({
        title: '服务器出现错误！',
        icon: 'error'
      })
    })
  },
  getFactories() {
    const that = this;
    const url = "https://logisticsengineering.gree.com/andon_system/get_factory_list";
    this.get(url).then((res) => {
      let options = []
      for (let i = 0; i < res.data.length; i++) {
        let option = {};
        option.value = res.data[i];
        option.text = res.data[i];
        options.push(option);
      }
      that.setData({
        factories: options
      })
      that.getLines();
      console.log(res)
    }).catch((err) => {
      console.log(err)
    })
  }, 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getFactories();
    this.getDatas()
    taskId = setInterval(this.getDatas, 5000)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    clearInterval(taskId)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})